﻿//NicEdit event handlers
var cmsNicPanel = null;
var myNicEditor = null;

function nicEdit_InsertImage(src, alt, width, height) {
    if (!cmsNicPanel.im) {
        var tmp = 'javascript:nicImTemp();';
        cmsNicPanel.ne.nicCommand("insertImage", tmp);
        cmsNicPanel.im = cmsNicPanel.findElm('IMG', 'src', tmp);
    }
    if (cmsNicPanel.im) {
        cmsNicPanel.im.setAttributes({
            src: src,
            alt: alt
        });
        if (width.indexOf('px') == -1)
            width = width + 'px';
        if (height.indexOf('px') == -1)
            height = height + 'px';
        cmsNicPanel.im.setStyle({
            width: width,
            height: height
        });
    }
    cmsNicPanel.removePane();
}

function nicEdit_InsertFile(src, target) {
    if (!cmsNicPanel.ln) {
        var tmp = 'javascript:nicTemp();';
        cmsNicPanel.ne.nicCommand("createlink", tmp);
        cmsNicPanel.ln = cmsNicPanel.findElm('A', 'href', tmp);
    }
    if (cmsNicPanel.ln) {
        cmsNicPanel.ln.setAttributes({
            href: src,
            title: src,
            target: target
        });
    }
    cmsNicPanel.removePane();
}

var cmsCtrlPressed = false;

$(window).keydown(function(event) {
    if (event.ctrlKey) {
        cmsCtrlPressed = true;
    }
    return true;
});

$(window).keyup(function(event) {
    cmsCtrlPressed = false;
    return true;
});

// Content module editor

function cmsContentModuleEditor_DefaultSave(moduleElement, nicEditor) {
    try {
        if ($(moduleElement).data('config').Content != nicEditor.getContent()) {
            $(moduleElement).data('config').Content = nicEditor.getContent();
            cmsSaveDraft();
        }
    } catch (e) {
    }
}

function cmsContentModuleEditor_DefaultSetup(moduleElement, nicEditor) {
    $(moduleElement).data('config').Content = nicEditor.getContent();
}

var cmsContentModuleEditor_CheckTargetCallback = null;
$().ready(function() {
    $(window).click(function(evt) {
        if (!evt && window.event) {
            evt = window.event;
        }
        if (evt) {
            var lastClickInNicEditPanel = $(evt.target).parents('.nicEdit-pane').length > 0 || $(evt.target).parents('.nicEdit-panel').length > 0;
            if (cmsContentModuleEditor_CheckTargetCallback != null) {
                cmsContentModuleEditor_CheckTargetCallback(lastClickInNicEditPanel);
                cmsContentModuleEditor_CheckTargetCallback = null;
            }
        }
        return true;
    });
});

jQuery.fn.cmsContentModuleEditor = function(opts) {
    return this.each(function() {
        if (!$(this).hasClass('cmsInherited')) {
            $('#cmsNicPanelContainer').remove();
            cmsNicPanel = $('<div id="cmsNicPanelContainer"><div id="cmsNicPanel"></div></div>').appendTo($('body'));
            myNicEditor = new nicEditor({ fullPanel: true }).panelInstance('cmsNicPanel');
            $('#cmsNicPanelContainer').hide(0);

            if (opts.onSetup == null || opts.onSetup == undefined) {
                opts = $.extend(opts, { onSetup: cmsContentModuleEditor_DefaultSetup });
            }

            if (opts.onSave == null || opts.onSave == undefined) {
                opts = $.extend(opts, { onSave: cmsContentModuleEditor_DefaultSave });
            }

            $(this).click(function() {

                $('#cmsNicPanelContainer').show(0).css('top', parseInt((parseInt($(window).scrollTop()) + 47)) + 'px');
                $('#cmsNicPanelContainer > div').width($('body').width());
                $('.nicEdit-panelContain').css('border-right', '0px').css('border-left', '0px');

                if (!nicEditors.findEditor(this.id)) {
                    $('#cmsNicPanelContainer').show(0).css('top', '47px').scrollFollow({ speed: 250, offset: 47 });
                    try {
                        var contentModuleNicEditor = myNicEditor.addInstance(this.id);
                        opts.onSetup(this, nicEditors.findEditor(this.id));
                        this.focus();
                    }
                    catch (e) { }
                }
            }).blur(function() {
                // Blur happens before click, so we need to find out if the click was in a NicEdit pane

                var cmsContentModuleEditor_CheckTargetCallbackElement = this;
                var cmsContentModuleEditor_CheckTargetCallbackOnSave = opts.onSave;
                cmsContentModuleEditor_CheckTargetCallback = function(r) {
                    if (!r) {
                        $('#cmsNicPanelContainer').hide(0);
                        cmsContentModuleEditor_CheckTargetCallbackOnSave(cmsContentModuleEditor_CheckTargetCallbackElement, nicEditors.findEditor(cmsContentModuleEditor_CheckTargetCallbackElement.id));
                        myNicEditor.removeInstance(cmsContentModuleEditor_CheckTargetCallbackElement.id);
                    }
                };
                
                // Hack to force IE to call method
                if (!jQuery.support.noCloneEvent)
                    cmsContentModuleEditor_CheckTargetCallback(false);
            });
        }
    });
};